<?php

namespace app\api\controller\minishop;

use app\common\controller\Api;
use Think\Db;

use app\admin\model\minishop\Address as AddressModel;


/**
 * 首页接口
 */
class Address extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new AddressModel();
    }


    
    public function getAddress()
    {
        // 从数据库读取数据
        $datas = $this->model->order('id desc')->select();

        foreach($datas as $key=>$val){
            $datas[$key]['taglist'] = explode(',',$val['tag']);
        }

        
        // 返回成功响应
        $this->success('请求成功', $datas);
    }

    /**
     * 删除地址
     * @throws \think\Exception
     */
    public function delAddress()
    {
        $id = $this->request->post('id');

        if(!$id){
           $this->error('没有传参');
        }

        if(!$this->model->where('id', $id)->find()){
            $this->error('数据不存在');
        }
        
        // 从数据库读取数据
        $data = $this->model->where('id', $id)->delete();
        // echo $this->model->getLastSql();
        // 返回成功响应，包含处理后的数据
        $this->success('请求成功', $data);
    }


    /**
     * 地址详情
     * @throws \think\Exception
     */
    public function detailAddress()
    {
        $id = $this->request->post('id');
        
        if(!$id){
            $this->error('参数不正确');
        }
        
        // echo $id;
        // 从数据库读取数据
        $data = Db::name('minishop_address')->where('id', $id)->find();

        if(!$data){
            $this->error('没有对应地址');
        }

        // 返回成功响应，包含处理后的数据
        $this->success('请求成功', $data);
    }


    /**
     * 编辑地址
     * @throws \think\Exception
     */
    public function editAddress()
    {

        $id = $this->request->param('id');
        $userAddress = AddressModel::where('id', $id)->find();
        if (!$userAddress) {
            $this->error(__('No Results were found'));
        }

        $params = $this->request->only([
            'city','tag','name', 'phone', 'province_name', 'city_name', 'area_name', 'address', 'is_default'
        ]);

        // print_r(params);

        $params['user_id'] = 1;//$user->id;
        // $this->svalidate($params, ".add");

        // $params = $this->getAreaIdByName($params);

        Db::transaction(function () use ($params,$userAddress) {
           
            $userAddress->save($params);

            // echo $userAddress->getLastSql();

        });

        $this->success('保存成功');
    }



    /*
    *指导价格
    */
    public function addAddress()
    {

        // $user = auth_user();

        $params = $this->request->only([
            'city','tag','name', 'phone', 'province_name', 'city_name', 'area_name', 'address', 'is_default'
        ]);
        // print_r($params);
        $params['user_id'] = 1;//$user->id;
        // $this->svalidate($params, ".add");

        // $params = $this->getAreaIdByName($params);

        // print_r($params);

        // var_dump(params);
        

        // if(trim(params['name']) !== ''){
        //     $this->error('用户名不能为空');
        // }

        // if(empty(trim(string: params['name']))){
        //     $this->error('用户名不能为空');
        // }
   
        

        Db::transaction(function () use ($params) {
            $userAddress = new AddressModel();
            $userAddress->save($params);
            // echo $this->model->getLastSql();
            
        });

        $this->success('保存成功');
    }
}